<%@ page language="java" import="java.util.*" import="java.sql.*" contentType="text/html; charset=UTF-8"
         pageEncoding="UTF-8"%>
<%@ page import="com.example.demo2.DBUtil" %>
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>后台管理系统</title>
</head>
<link rel="stylesheet" href="css/bootstrap.css">
<body>
<style>
    .center{display: flex !important;align-content: center;justify-content: center;align-items: center;}
</style>
<div style="display: flex; justify-content: center;flex-direction: column;align-items: center;">
    <%@include file="admin_header.jsp"%>
    <%
    request.setCharacterEncoding("UTF-8");
    String y_id = request.getParameter("y_id");
    %>
    <div style="margin-top: 20px">
        <div style="display: flex;justify-content: space-between;">
            <button type="button" class="btn btn-primary" style="margin-bottom: 20px" data-toggle="modal" data-target="#exampleModal">
                添加观影厅</button>
            <a href="ad_fyt.jsp?y_id=<%=y_id%>"><button type="button" class="btn btn-success" style="margin-bottom: 20px;">刷新</button></a>
        </div>

        <ul class="list-group list-group-horizontal">
            <li class="list-group-item" style="width: 50px">ID</li>
            <li class="list-group-item"style="width: 150px">电影院名称</li>
            <li class="list-group-item"style="width: 150px">观影厅名称</li>
            <li class="list-group-item"style="width: 150px">播放电影名</li>
            <li class="list-group-item"style="width: 150px">开始时间</li>
            <li class="list-group-item"style="width: 150px">结束时间</li>
            <li class="list-group-item"style="width: 150px">语言</li>
            <li class="list-group-item"style="width: 150px">付款金额</li>
            <li class="list-group-item"style="width: 150px">操作</li>
        </ul>
        <%
            request.setCharacterEncoding("UTF-8");
            String a_name = request.getParameter("a_name");
            String a_y_id = request.getParameter("a_y_id");
            String a_g_id = request.getParameter("a_g_id");
            String a_time = request.getParameter("a_time");
            String a_o_time = request.getParameter("a_o_time");
            String a_language = request.getParameter("a_language");
            String a_qian = request.getParameter("a_qian");

            String u_id = request.getParameter("u_id");
            String u_name = request.getParameter("u_f_name");
            String u_y_id = request.getParameter("u_y_id");
            String u_g_id = request.getParameter("u_g_id");
            String u_time = request.getParameter("u_time");
            String u_o_time = request.getParameter("u_o_time");
            String u_language = request.getParameter("u_language");
            String u_qian = request.getParameter("u_qian");
            String d_id = request.getParameter("d_id");
            Connection cn = null;
            PreparedStatement ps = null;

            ResultSet rs = null;
            String sql = "";
            cn = DBUtil.getConnection();
            sql = "SELECT f.id,c.name c_name,f.name f_name,f.time,f.o_time,f.`language`,f.qian, g.name g_name from fyt f JOIN cinema c ON f.c_id = c.id \n" +
                    "JOIN goods g ON f.g_id = g.id WHERE c.id =" + y_id;
            ps = cn.prepareStatement(sql);
            rs = ps.executeQuery();
            String error = "";
            if (a_name != null && a_g_id !=null){
                int n=0;
                sql = "insert into fyt(c_id,time,o_time,language,name,qian,g_id) values(?,?,?,?,?,?,?)";
                ps = cn.prepareStatement(sql);
                ps.setObject(1, y_id);
                ps.setObject(2, a_time);
                ps.setObject(3, a_o_time);
                ps.setObject(4, a_language);
                ps.setObject(5, a_name);
                ps.setObject(6, a_qian);
                ps.setObject(7, a_g_id);
                n= ps.executeUpdate();
                response.sendRedirect("ad_fyt.jsp?y_id="+y_id);
            }
            if (u_id != null){
                int n=0;
                sql = sql = "UPDATE fyt SET c_id ="+y_id+", time =\""+ u_time +"\" "+ ",o_time=\"" + u_o_time +
                        "\",language=\"" + u_language +
                        "\",name=\""+ u_name + "\",qian=" + u_qian + ",g_id=" + u_g_id + " WHERE id=" + u_id;
                System.out.println(sql);
                ps = cn.prepareStatement(sql);
                n= ps.executeUpdate();
                response.sendRedirect("ad_fyt.jsp?y_id="+y_id);

            }
            if (d_id != null){
                int n=0;
                sql = "DELETE FROM `gp`.`fyt` WHERE `id` = " + d_id;
                ps = cn.prepareStatement(sql);
                n= ps.executeUpdate();
                response.sendRedirect("ad_fyt.jsp?y_id="+y_id);
            }
            while(rs.next()){
                int id = rs.getInt("id");
                String c_name = rs.getString("c_name");
                String f_name = rs.getString("f_name");
                String time = rs.getString("time");
                String o_time = rs.getString("o_time");
                String language = rs.getString("language");
                String qian = rs.getString("qian");
                String g_name = rs.getString("g_name");
//                System.out.println(123);
        %>
        <ul class="list-group list-group-horizontal-sm">
            <li class="list-group-item center"style="width: 50px;"><%=id%></li>
            <li class="list-group-item center"style="width: 150px"><%=c_name%></li>
            <li class="list-group-item center"style="width: 150px"><%=f_name%></li>
            <li class="list-group-item center"style="width: 150px"><%=g_name%></li>
            <li class="list-group-item center"style="width: 150px"><%=time%></li>
            <li class="list-group-item center"style="width: 150px"><%=o_time%></li>
            <li class="list-group-item center"style="width: 150px"><%=language%></li>
            <li class="list-group-item center"style="width: 150px"><%=qian%></li>
            <li class="list-group-item center"style="width: 150px">
                <button type="button" class="btn btn-primary btn-sm"
                        data-toggle="modal" data-target="#upfytModal<%=id%>"
                        style="margin-bottom: 20px;margin-right: 5px">修改</button>
                <a href="ad_fyt.jsp?y_id=<%=y_id%>&d_id=<%=id%>" type="button" class="btn btn-info btn-sm" style="margin-bottom: 20px">删除</a>
            </li>
        </ul>
            <%@include file="up_fyt.jsp"%>

        <%
            }
        %>
    </div>


    <div class="modal fade" id="exampleModal" tabindex="-1" aria-labelledby="exampleModalLabel" aria-hidden="true">
        <div class="modal-dialog modal-dialog-centered modal-dialog-scrollable">
            <div class="modal-content">
                <div class="modal-header">
                    <h5 class="modal-title" id="exampleModalLabel">添加观影厅</h5>
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                        <span aria-hidden="true">&times;</span>
                    </button>
                </div>


                <div class="modal-body">
                    <form action="ad_fyt.jsp?y_id=<%=y_id%>" method="post">
                        <div class="form-group" style="display: none">
                            <label for="c_id">影院名称:</label>
                            <input type="text" name="a_y_id" value="<%=y_id%>" required class="form-control" id="c_id">
                        </div>
                        <div class="form-group">
                            <label for="name">观影厅名称:</label>
                            <input type="text" name="a_name"  required class="form-control" id="name">
                        </div>
                        <div class="form-group">
                            <label for="g_id">播放的电影:</label>
                            <select class="form-control" name="a_g_id" id="g_id">
                                <%
                                    String[] s4 = new String[]{"改签","退票","改签/退票"};
                                    sql = "SELECT id,name from goods";
                                    ps = cn.prepareStatement(sql);
                                    rs = ps.executeQuery();
                                    while(rs.next()){
                                        int o_id = rs.getInt("id");
                                        String o_name = rs.getString("name");
                                %>
                                <option value="<%=o_id%>"><%=o_name%></option>
                                <%
                                    }%>
                            </select>
                        </div>
                        <div class="form-group">
                            <label for="time">开始时间:</label>
                            <input name="a_time" required type="time" id="time">
                        </div>
                        <div class="form-group">
                            <label for="o_time">结束时间:</label>
                            <input name="a_o_time" required type="time" id="o_time">
                        </div>
                        <div class="form-group">
                            <label for="language">语言:</label>
                            <input type="text" name="a_language" required class="form-control" id="language">
                        </div>
                        <div class="form-group">
                            <label for="qian">最低金额:</label>
                            <input type="number" name="a_qian" required class="form-control"
                                   id="qian">
                        </div>
                        <div class="modal-footer">
                            <button type="button" class="btn btn-secondary" data-dismiss="modal">取消</button>
                            <button type="submit" class="btn btn-primary">确认</button>
                        </div>
                    </form>
                </div>

            </div>
        </div>
    </div>

</div>
<script src="js/jQuery.js"></script>
<script src="js/bootstrap.js"></script>

</body>
</html>